package com.m3839.ym.demo

import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import com.m3839.sdk.ym.YouthModelConfig
import com.m3839.sdk.ym.YouthModelSDK
import com.m3839.sdk.ym.core.YMHeartController

class MainActivity : AppCompatActivity() {

    private val ORIENTATION = arrayOf(
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,  // 1，竖屏
        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE // 0，横屏
    )

    private val ORIENTATION_NAMES = arrayOf(
        "竖屏",
        "横屏"
    )

    private lateinit var orientationSp: Spinner

    private lateinit var mSp: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 去除标题
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        // 设置显示全屏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        mSp = getSharedPreferences("sdk_sp", MODE_PRIVATE)
        // 设置DemoActivity的方向
        setDemoOrientation()

        setContentView(R.layout.activity_main)

        orientationSp = findViewById(R.id.spinner_orientation)
        initOrientationSpinner()

        val config = YouthModelConfig.build {
            normalPlayTime = 0.1f
            holidayPlayTime = 3f
            nightStartTime = 22  //夜间模式开始时间点，12-23
            nightEndTime = 8     //夜间模式开始时间点，12-23
            defaultPassword = "3839"  //设置默认密码
            contact = "test@qq.com"  //设置联系邮箱
            isLand = getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE //是否横屏展示
            workdayList = getWorkDays() //设置法定节假日，list的单项格式是：yyyy-mm-dd
            holidayList = getHolidaysDays() //设置法定工作日 list的单项格式是：yyyy-mm-dd
        }

        YouthModelSDK.initSDK(this@MainActivity, config) { code, message ->
            run {
                if (code == 1000) {
                    showToast(message)
                    finish()
                }
            }
        }
    }

    /**
     * 测试SDK界面的方向设置
     *
     * 为了在Demo中测试横竖屏，提供Spinner控件设置方向配置，并保持着SharedPreference中
     * 实际接入直接设置OperateCenterConfig
     * .Builder(this)。setOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
     *
     * 注意：横竖屏设置都需要重启Demo才能生效; 测试旋转屏幕时要开启“自动旋转屏幕”
     */
    private fun setDemoOrientation() {
        val ori = getOrientation()
        requestedOrientation = ori
    }

    fun initOrientationSpinner() {
        val adapter: ArrayAdapter<String> = ArrayAdapter<String>(
            this,
            android.R.layout.simple_spinner_item,
            ORIENTATION_NAMES
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        orientationSp.adapter = adapter
        orientationSp.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
                Log.d("m3839", "Set orientation as ${ORIENTATION_NAMES[pos]}" )
                saveOrientation(pos)
            }

            override fun onNothingSelected(arg0: AdapterView<*>?) {}
        }

        // 这里设置默认方向为横屏，SDK内部的默认方向也是
        orientationSp.setSelection(mSp.getInt("orientation", 0))
    }

    private fun saveOrientation(pos: Int) {
        mSp.edit().putInt("orientation", pos).apply()
    }

    private fun getOrientation(): Int {
        var pos = mSp.getInt("orientation", 0)
        if (pos < 0 || pos > 3) pos = 0
        return ORIENTATION[pos]
    }

    private fun getWorkDays(): MutableList<String> {
        val workList = mutableListOf<String>()
        workList.add("2021-06-14");
        workList.add("2021-07-01")
        return workList
    }

    private fun getHolidaysDays(): MutableList<String> {
        val workList = mutableListOf<String>()
        workList.add("2021-06-14");
        workList.add("2021-08-14")
        return workList
    }

    private fun showToast(text: String) {
        Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
    }
}